﻿/*
回复问题
*/
//
angular.module('replayQuestionModel', ['vModal', 'loadModule', 'Common', 'interlocutionService'])
// 设置焦点 标签用
.directive('setFocus', function () {
    return function (scope, element) {
        element[0].focus();
    };
})
 .directive("replayQuestion", ["$rootScope", "$stateParams", "$timeout", "Msg", "MsgCon", "sysUserInfo", "interlocutionservice", function ($rootScope, $stateParams, $timeout, Msg, MsgCon, sysUserInfo, IS) {
     return {
         restrice: "AE",
         scope: {
             item: "=",   // 保存对象
             uploader: "=", //上传文件的对象
             onClose: "&"    //关闭窗口                          
         },
         templateUrl: "/system/directive/interlocution/replay_question.html",
         link: function ($scope, element, attr) {
             $scope.isHideFun = function () {
                 $scope.onClose();
             }
             if ($scope.item.spare == undefined) {
                 $scope.item.spare = [];
             }
             if (typeof ($scope.item.spare) == "string") {
                 $scope.item.spare = JSON.parse($scope.item.spare);
             }
             //查询的参数
             $scope.params = {
                 qaid: $scope.item.qaid,
                 knowledgeid: "",
                 userid: sysUserInfo.getuserinfo().user_ID,
                 orgid: sysUserInfo.getuserinfo().organization_ID,
                 parentId: "0",
                 searchTxt: "",
                 orderby: "1",
                 pageIndex: 1,
                 pageSize: 10
             };
             $scope.Loading = false;
             /**********************************查询***********************************/
             $scope.replayList = [];
             $scope.numCount = 0;

             //加载更多
             $scope.loadMore = function () {
                 $scope.params.pageIndex = $scope.params.pageIndex + 1;
                 $scope.getList();
             }

             //获取回复的列表
             $scope.getList = function (item) {
                 if (item == undefined) {
                     $scope.params.parentId = 0;
                 }
                 IS.findReplay($scope.params).then(function (response) {
                     $scope.Loading = false;
                     if (response.errorcode == "0") {
                         if ($scope.params.parentId != 0) {
                             item.ziList = response.datas;
                             item.ziList.forEach(function (data, index) {
                                 if (data.pointNum == undefined) {
                                     data.pointNum = 0;
                                 }
                                 data.showEdit = false;
                             });
                             item.numCount = response.numCount;
                         } else {
                             $scope.replayList = $scope.replayList.concat(response.datas);
                             $scope.replayList.forEach(function (data, index) {
                                 if (data.pointNum == undefined) {
                                     data.pointNum = 0;
                                 }
                                 data.showEdit = false;
                                 if (data.spare != undefined && data.spare.length > 0 && typeof (data.spare) == "string") {
                                     data.spare = JSON.parse(data.spare);
                                 }
                             });
                             $scope.numCount = response.numCount;
                         }
                     } else {
                         Msg.error("查询异常!")
                     }
                 }, function errorCallback() {
                     $scope.Loading = false;
                     console.log("连接服务器失败！");
                 });
             }

             //查询回复列表
             $scope.getList();
             //回复内容
             $scope.replayContent = "";
             //附件
             $scope.fileObj = {};
             //初始新增
             $scope.initAdd = function () {
                 //新增的对象
                 $scope.replayInfo = {
                     qaId: $scope.item.qaid,
                     areplayFid: "0",
                     areplayContent: "",
                     replyUserId: sysUserInfo.getuserinfo().user_ID,
                     replyUserName: sysUserInfo.getuserinfo().user_Name,
                     replyAvatar: sysUserInfo.getuserinfo().user_Img,
                     replyOrgId: sysUserInfo.getuserinfo().organization_ID,
                     spare: []//附件的json
                 };
             }
             $scope.initAdd();
             //新增回复的方法
             $scope.addReplay = function (item) {
                 //新增
                 if (item == undefined) {
                     if ($scope.replayContent.length <= 0) {
                         Msg.warning("请输入回复内容！");
                         return;
                     }
                     $scope.replayInfo.areplayContent = $scope.replayContent;
                     if ($scope.fileObj.name != undefined && $scope.fileObj.name.length > 0) {
                         $scope.replayInfo.spare.push($scope.fileObj);
                     }
                 }
                 $scope.replayInfo.spare = JSON.stringify($scope.replayInfo.spare);
                 IS.saveReplay({ data: JSON.stringify($scope.replayInfo) }).then(function (response) {
                     if (response.errorcode == "0") {
                         //var item = ;
                         //像子集里添加
                         if ($scope.replayInfo.areplayFid != "0") {
                             if (response.data.spare != undefined && response.data.spare.length > 0) {
                                 response.data.spare = JSON.parse(response.data.spare);
                             }
                             item.ziList.push(response.data);
                             item.replayContent = "";
                             //像回复列表里添加
                         } else {
                             if (response.data.spare != undefined && response.data.spare.length > 0) {
                                 response.data.spare = JSON.parse(response.data.spare);
                             }
                             $scope.replayList.push(response.data);
                             $scope.fileObj = {};
                             $scope.replayContent = "";
                         }
                         $scope.item.replyNum = $scope.item.replyNum + 1;
                         $scope.initAdd();
                     } else {
                         Msg.error("回复失败!")
                     }
                 }, function errorCallback() {
                     console.log("连接服务器失败！");
                 });
             }

             //设为取消满意
             $scope.setManyi = function (item) {
                 IS.setManyi({ replayid: item.areplayId, qaid: $scope.item.qaid }).then(function (response) {
                     if (response.errorcode == "0") {
                         //请求的是设为满意，需要取消其他的满意
                         if (!item.isSolve) {
                             $scope.replayList.forEach(function (data, index) {
                                 data.isSolve = false;
                             })

                         }
                         item.isSolve = !item.isSolve;
                         Msg.success("操作成功");
                     } else {
                         Msg.error("回复失败!")
                     }
                 }, function errorCallback() {
                     console.log("连接服务器失败！");
                 });
             }


             /*************************************删除*********************************************/
             //删除父
             $scope.delReplay = function (item) {
                 MsgCon.delnoicon().then(function (flag) {   // 删除弹框
                     if (flag) {
                         IS.delReplay({ replayid: item.areplayId, userid: sysUserInfo.getuserinfo().user_ID, qaid: $scope.item.qaid }).then(function (response) {
                             if (response.errorcode == "0") {
                                 $scope.item.replyNum = $scope.item.replyNum - 1;
                                 Msg.success("删除成功");
                                 //移除
                                 $scope.replayList.forEach(function (data, index) {
                                     if (data.areplayId == item.areplayId) {
                                         $scope.replayList.splice(index, 1);
                                     }
                                 })
                                 $scope.numCount = $scope.numCount - 1;
                             } else {
                                 Msg.error("回复失败!")
                             }
                         }, function errorCallback() {
                             console.log("连接服务器失败！");
                         });
                     }
                 });

             }
             //删除子
             $scope.delZiReplay = function (item, id) {
                 MsgCon.delnoicon().then(function (flag) {   // 删除弹框
                     if (flag) {
                         IS.delReplay({ replayid: id, userid: sysUserInfo.getuserinfo().user_ID, qaid: $scope.item.qaid }).then(function (response) {
                             if (response.errorcode == "0") {
                                 item.ziList.forEach(function (data, index) {
                                     if (data.areplayId == id) {
                                         $scope.replayList.splice(index, 1);
                                     }
                                 });
                                 $scope.item.replyNum = $scope.item.replyNum - 1;
                                 $scope.numCount = $scope.numCount - 1;
                                 Msg.success("删除成功");
                             } else {
                                 Msg.error("回复失败!")
                             }
                         }, function errorCallback() {
                             console.log("连接服务器失败！");
                         });
                     }
                 });

             }

             /********************************************附件***********************************************/
             $scope.fileObj = {}; //附件对象


             //添加附件
             $scope.addFile = function () {
                 document.getElementById("FJUpload").click();
             }
             var trainupfile = $scope.uploader;
             trainupfile.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {     // 选择文件后失败       0
                 console.info('onWhenAddingFileFailed', item, filter, options);
             };
             trainupfile.onAfterAddingFile = function (fileItem) {  // 选择文件后 （成功）   0          
                 console.info('onAfterAddingFile', fileItem);
                 if (trainupfile.isUploading) {
                     Msg.warning("有文件正在上传。。");
                     return;
                 } else {
                     var upfileparam = "?orgid=" + sysUserInfo.getuserinfo().organization_ID + "&userid=" + sysUserInfo.getuserinfo().user_ID;
                     //上传文件路径
                     fileItem.url = IS.uploadEnclosure(upfileparam);
                 }
             };
             trainupfile.onAfterAddingAll = function (addedFileItems) { // 选择文件后添加到队列   1 
                 console.info('onAfterAddingAll', addedFileItems);
                 if (trainupfile.isUploading) {
                     Msg.warning("有文件正在上传。。");
                     return;
                 } else {
                     trainupfile.uploadAll();
                 }
             };
             // 开始上传
             trainupfile.onBeforeUploadItem = function (item) {    // 2 上传之前一项
                 console.info('onBeforeUploadItem', item);
             };
             trainupfile.onProgressItem = function (fileItem, progress) {   // 3 上传进度
                 console.info('onProgressItem', fileItem, progress);
             };
             trainupfile.onProgressAll = function (progress) {      //4 队列进度
                 console.info('onProgressAll', progress);
             };
             trainupfile.onSuccessItem = function (fileItem, response, status, headers) {   // 5上传成功
                 console.info('onSuccessItem', fileItem, response, status, headers); //fileItem 请求信息 response: 返回值status: 返回状态  headers: 请求头 
                 //                 $scope.path = response.errormsg; //文件路径
                 //                 $scope.filename = fileItem.name; //文件名称
                 if (response.errorcode == 0) {
                     $scope.fileObj = { name: fileItem.file.name, path: response.errormsg, type: fileItem.file.name.split(".")[1] };
                     Msg.success("上传成功！");
                 } else if (response.errorcode == "10002") {
                     Msg.warning("文件过大，请重新选择！");
                 } else if (response.errorcode == "10001") {
                     Msg.warning("文件格式错误，请选择图片文件！");
                 } else {
                     Msg.error("上传失败！" + response.errorcode);
                 }
             };
             trainupfile.onErrorItem = function (fileItem, response, status, headers) {     // 5上传失败
                 console.info('onErrorItem', fileItem, response, status, headers);
             };
             trainupfile.onCancelItem = function (fileItem, response, status, headers) {    //5取消上传
                 console.info('onCancelItem', fileItem, response, status, headers);
             };
             trainupfile.onCompleteItem = function (fileItem, response, status, headers) {  //6     单文件上传完成
                 console.info('onCompleteItem', fileItem, response, status, headers);

             };
             trainupfile.onCompleteAll = function () {  // 6 单文件上传完成或者队列上传完成的时候
                 console.info('onCompleteAll');
             };
             /*********************************************文件上传结束**********************************************/
         }
     }
 } ]);
